package cn.itcast.bos.web.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.constant.Constant;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.service.FixedAreaService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.crm.domain.Customer;
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
@Controller
public class FixedAreaAction extends BaseAction<FixedArea> {

	@Autowired
	private FixedAreaService fixedAreaService;
	
	
	//保存fixedarea信息
	@Action(value="fixedArea_save",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String save(){
		fixedAreaService.save(model);
		
		return SUCCESS;
	}
	
	//分页查询展示
	@Action(value="fixedArea_pageQuery",results={@Result(name="success",type="json")})
	public String pageQuery(){
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate predicate = null;
				List<Predicate> list = new ArrayList<>();
				
				if(StringUtils.isNotBlank(model.getId())){
					Predicate p = cb.equal(root.get("id").as(String.class), model.getId());
					list.add(p);
				}
				if(StringUtils.isNotBlank(model.getCompany())){
					Predicate p  = cb.like(root.get("company").as(String.class), model.getCompany());
					list.add(p);
				}
				if(list != null && list.size() != 0){
					Predicate [] arr = new Predicate[list.size()];
					predicate = cb.and(list.toArray(arr));
					
				}
				
				
				return predicate;
			}
		};
		
		Pageable pageable = new PageRequest(page-1, rows);
		
		Page<FixedArea> page = fixedAreaService.pageQuery(specification,pageable);
		this.pushPageToValueStack(page);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_findNoAssociationCustomers",results={@Result(name="success",type="json")})
	public String findNoAssociationCustomers(){
		
		Collection<? extends Customer> collection = WebClient.create(Constant.CRM_MANAGEMENT_URL+"/services").path("/customerService/noAssiciationCustomers")
		.accept(MediaType.APPLICATION_JSON_TYPE).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}
	
	@Action(value="fixedArea_findAssociationCustomers",results={@Result(name="success",type="json")})
	public String findAssociationCustomers(){
		Collection<? extends Customer> collection = WebClient.create(Constant.CRM_MANAGEMENT_URL+"/services").path("/customerService/hasAssiciationFixedAreaCustomers/"+model.getId()).accept(MediaType.APPLICATION_JSON_TYPE)
		.type(MediaType.APPLICATION_JSON_TYPE).getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		
		return SUCCESS;
	}
	//接收关联定区的客户
	private String [] customerIds;
	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	//关联定区和客户
	@Action(value="fixedArea_associationCustomersToFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCustomersToFixedArea(){
		//更新前根据定区id,置空原外键
		WebClient.create(Constant.CRM_MANAGEMENT_URL+"/services").path("/customerService/clearFixedAreaId/"+model.getId()).type(MediaType.APPLICATION_JSON_TYPE)
		.put(null);
		
		String customerIdStr =  StringUtils.join(customerIds, ",");
		WebClient.create(Constant.CRM_MANAGEMENT_URL+"/services/customerService/associationCustomersToFixedArea?customerIdStr="+customerIdStr+"&fixedAreaId="+model.getId())
		.put(null);
		
		return SUCCESS;
	}
	
	private String courierId;
	private String takeTimeId;
	public void setCourierId(String courierId) {
		this.courierId = courierId;
	}
	public void setTakeTimeId(String takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	//关联快递员窗口
	@Action(value="fixedArea_associationCourierToFixedArea",results={@Result(name="success",type="redirect",location="./pages/base/fixed_area.html")})
	public String associationCourierToFixedArea(){
		fixedAreaService.associationCourierToFixedArea(model.getId(),courierId,takeTimeId);
		
		return SUCCESS;
	}
}




